package com.ajouroid.smartcoworker;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Team {

	String type = "presentation";
	String id;
	String leader;
	ArrayList<String> member;
	ArrayList<String> slides;
	
	public Team(String _id, String _leader)
	{
		id = _id;
		leader = _leader;
		member = new ArrayList<String>();
		slides = new ArrayList<String>();
	}
	
	public void setType(String _type)
	{
		type = _type;
	}
	
	public static Team parseFromString(String json)
	{
		Team t = null;
		try {
			JSONObject obj = new JSONObject(json);
			
			String id = obj.getString("id");
			String leader = obj.getString("leader");
			String type = obj.getString("type");
			
			t = new Team(id,leader);
			
			if (type != null)
			{
				t.setType(type);
			}
			
			JSONArray memberArr = obj.getJSONArray("members");
			JSONArray slideArr = obj.getJSONArray("slides");
			
			ArrayList<String> member = new ArrayList<String>();
			ArrayList<String> slides = new ArrayList<String>();
			
			for (int i=0; i<memberArr.length(); i++)
			{
				member.add(memberArr.getString(i));
			}
			for (int i=0; i<slideArr.length(); i++)
			{
				slides.add(slideArr.getString(i));
			}
			
			t.setMembers(member);
			t.setSlides(slides);
		} catch (JSONException e) {
			e.printStackTrace();
			Log.d("Team", json);
		}
		
		return t;
	}
	
	public void addMember(String memberId)
	{
		member.add(memberId);
	}
	
	public void parseMembers(String memberStr)
	{
		String[] members = memberStr.split(";");
		
		for (int i=0; i<members.length; i++)
		{
			if (!members[i].equals(""))
				member.add(members[i]);
		}
	}
	
	public void parseSlides(String slideStr)
	{
		String[] slideList = slideStr.split(";");

		for (int i=0; i<slideList.length; i++)
		{
			if (!slideList[i].equals(""))
				slides.add(slideList[i]);
		}
	}
	
	public void setSlides(ArrayList<String> slideArr)
	{
		slides = slideArr;
	}
	
	public ArrayList<String> getSlides()
	{
		return slides;
	}
	
	public void setMembers(ArrayList<String> memberArr)
	{
		member = memberArr;
	}
	
	public String getId()
	{
		return id;
	}
	
	public ArrayList<String> getMembers()
	{
		return member;
	}
	
	public String getMemberStr()
	{
		String list = "";
		
		for (int i=0; i<member.size(); i++)
			list += member.get(i) + ";";
		
		return list;
	}
	
	public String getSlideStr()
	{
		String list = "";
		
		for (int i=0; i<slides.size(); i++)
			list += slides.get(i) + ";";
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public String getJSONString()
	{		
		JSONObject obj = new JSONObject();
		
		try {
			obj.put("id", id);
			obj.put("leader", leader);
			JSONArray memberArr = new JSONArray();
			
			for (int i=0; i<member.size(); i++)
				memberArr.put(member.get(i));
			
			obj.put("members", memberArr);
			
			JSONArray slideArr = new JSONArray();
			for (int i=0; i<slides.size(); i++)
			{
				slideArr.put(slides.get(i));
			}
			obj.put("pages", slides.size());
			obj.put("slides", slideArr);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		
		return obj.toString();
	}
}
